package ln.javatest.day13.demo02;

import java.util.HashMap;
import java.util.Scanner;

public class Test {
    static Scanner sc = new Scanner(System.in);
    static HashMap<String, User> hashMap = new HashMap<>();

    public static void main(String[] args) {
        hashMap.put("1234", new User("zs", "1234"));
        while (true) {
            menu();
        }
    }

    public static void menu() {
        System.out.println("************银行系统************");
        System.out.println("1.管理员");
        System.out.println("2.用户");
        System.out.println("请输入要选择的菜单对应的编号");
        String nextInt = sc.next();
        list(nextInt);
    }

    public static void list(String n) {
        while (true) {
            switch (n) {
                case "1":

                    break;
                case "2":
                    System.out.println("1.注册");
                    System.out.println("2.登录");
                    String next = sc.next();
                    switch (next) {
                        case "1":
                            Added();
                            break;
                        case "2":
                            while (true) {
                                System.out.println("输入账户");
                                String next2 = sc.next();
                                System.out.println("输入密码");
                                String next3 = sc.next();
                                if (hashMap.containsKey(next2)) {
                                    if (hashMap.get(next2).pass.equals(next3)) {
                                        System.out.println("登录成功");
                                        list2(next2);
                                        break;
                                    } else {
                                        System.out.println("密码错误登录失败");
                                    }
                                } else {
                                    System.out.println("账户不存在登录失败");
                                }
                            }
                    }
            }
        }

    }

    public static void list2(String a) {
        a: while (true) {
            System.out.println("1.存款");
            System.out.println("2.取款");
            System.out.println("3.查询余额");
            System.out.println("4.转账");
            System.out.println("5.修改密码");
            System.out.println("6.退出");
            String next = sc.next();
            switch (next) {
                case "1":
                    System.out.println("请输入要存入的钱数");
                    int nextInt = sc.nextInt();
                    hashMap.get(a).money += nextInt;
                    break;
                case "2":
                    System.out.println("请输入要取出的钱数");
                    int nextInt2 = sc.nextInt();
                    if (hashMap.get(a).money>=nextInt2) {
                        hashMap.get(a).money-=nextInt2;
                    }else{
                        System.out.println("余额不足");
                    }
                    break;
                case "3":
                    System.out.println("当前余额为:" + hashMap.get(a).money);
                    break;
                case "4":
                    System.out.println("请输入你要转入的账户");
                    String next3 = sc.next();
                    if(hashMap.get(a)==hashMap.get(next3)){
                        System.out.println("不能给自己转账");
                        break;
                    }
                    if(hashMap.containsKey(next3)){
                        System.out.println("请输入要转入的金额");
                        int nextInt3 = sc.nextInt();
                        if(hashMap.get(a).money>=nextInt3){
                            System.out.println("请输入密码");
                            String next2 = sc.next();
                            if(hashMap.get(a).pass.equals(next2)){
                                hashMap.get(a).money-=nextInt3;
                                hashMap.get(next3).money+=nextInt3;
                            }else{
                                System.out.println("密码错误");
                            }
                        }else{
                            System.out.println("余额不足");
                        }

                    }else{
                        System.out.println("账户不存在");
                    }
                    break;
                case "5":
                    System.out.println("请输入修改后的密码");
                    String next2 = sc.next();
                    hashMap.get(a).pass=next2;
                    break;
                case "6":
                    break a;
            }
        }
    }

    // 添加用户
    public static void Added() {
        while (true) {
            System.out.println("请输入要注册的账号名");
            String next = sc.next();
            if (hashMap.containsKey(next)) {
                System.out.println("该账户已存在");
                continue;
            }
            System.out.println("请输入密码");
            String next2 = sc.next();
            System.out.println("请输入姓名");
            String next3 = sc.next();
            hashMap.put(next, new User(next3, next2));
            break;
        }
    }

}

